package com.chenl.provider.题目;

import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.util.concurrent.*;

public class Day3 {
    public static void main(String[] args) throws BrokenBarrierException, InterruptedException {
        long start = System.currentTimeMillis();
        ExecutorService executorService = Executors.newFixedThreadPool(2);

        executorService.shutdown();
        long end = System.currentTimeMillis();
        System.out.println("执行耗时:"+ BigDecimal.valueOf(end).subtract(BigDecimal.valueOf(start)).divide(BigDecimal.valueOf(1000))+"秒");

    }

    @NoArgsConstructor
    public static class MyThread1 implements Callable{
        private CyclicBarrier cyclicBarrier;
        private CountDownLatch countDownLatch;
        private String name;

        public MyThread1(CyclicBarrier cyclicBarrier){
            this.cyclicBarrier=cyclicBarrier;
        }
        public MyThread1(String name,CountDownLatch countDownLatch){
            this.countDownLatch=countDownLatch;
            this.name=name;
        }
        public MyThread1 setName(String name){
            this.name=name;
            return this;
        }
        public MyThread1 setCyclic(CyclicBarrier cyclicBarrier){
            this.cyclicBarrier=cyclicBarrier;
            return this;
        }
        @Override
        public Object call() throws Exception {
            Thread.sleep(3000);
            System.out.println("name:"+name);
            cyclicBarrier.await();
            return null;
        }
    }
    public static class MyThread2 implements Callable{
        private CyclicBarrier cyclicBarrier;
        private CountDownLatch countDownLatch;
        private String name;

        public MyThread2(CyclicBarrier cyclicBarrier){
            this.cyclicBarrier=cyclicBarrier;
        }
        public MyThread2(String name,CountDownLatch countDownLatch){
            this.countDownLatch=countDownLatch;
            this.name=name;
        }
        public MyThread2 setName(String name){
            this.name=name;
            return this;
        }
        @Override
        public Object call() throws Exception {
            Thread.sleep(1000);
            System.out.println("name:"+name);
            cyclicBarrier.await();
            return null;
        }
    }
    @NoArgsConstructor
    public static class MyThread3 implements Callable{
        private CyclicBarrier cyclicBarrier;
        private CountDownLatch countDownLatch;
        private String name;

        public MyThread3(CyclicBarrier cyclicBarrier){
            this.cyclicBarrier=cyclicBarrier;
        }
        public MyThread3(String name,CountDownLatch countDownLatch){
            this.countDownLatch=countDownLatch;
            this.name=name;
        }
        public MyThread3 setName(String name){
            this.name=name;
            return this;
        }
        @Override
        public Object call() throws Exception {
            Thread.sleep(2000);
            System.out.println("name:"+name);
            countDownLatch.countDown();
            return null;
        }
    }

    @NoArgsConstructor
    public static class MyThread4 implements Runnable{
        private CyclicBarrier cyclicBarrier;
        private CountDownLatch countDownLatch;
        private String name;

        public MyThread4(CyclicBarrier cyclicBarrier){
            this.cyclicBarrier=cyclicBarrier;
        }
        public MyThread4(String name,CountDownLatch countDownLatch){
            this.countDownLatch=countDownLatch;
            this.name=name;
        }
        public MyThread4 setName(String name){
            this.name=name;
            return this;
        }
        @Override
        public void run()  {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("我执行咯");
//            countDownLatch.countDown();
        }
    }
}
